;; Non-robust linear algebra functions
;; Copyright (C) 2022  Rocks Mazama
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.linalg.transform.reflect)

(defn transform
  "Return a function that reflects a 2D vector about a line through the origin.
  The line is defined by the vector `l` = [lx ly].
  https://en.wikipedia.org/wiki/Reflection_(mathematics)#Reflection_across_a_line_in_the_plane"
  [[lx ly]]
  (if (zero? lx)
    (fn [[vx vy]] [(- vx) vy])
    (let [l·l (+ (* lx lx) (* ly ly))]
      (fn [[vx vy]]
        (let [v·l (+ (* vx lx) (* vy ly))
              s (* 2 (/ v·l l·l))]
          [(- (* s lx) vx)
           (- (* s ly) vy)])))))
